package com.ruoyi.oa.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.oa.domain.OaAdminAsset;
import com.ruoyi.oa.domain.bo.OaAdminEntryBo;
import com.ruoyi.oa.domain.vo.OaAdminEntryVo;
import com.ruoyi.oa.domain.vo.OaTotalAssetVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.vo.OaAdminAssetVo;
import com.ruoyi.oa.domain.bo.OaAdminAssetBo;
import com.ruoyi.oa.service.IOaAdminAssetService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资产管理
 *
 * @author ruoyi
 * @date 2023-08-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/adminAsset")
public class OaAdminAssetController extends BaseController {

    private final IOaAdminAssetService iOaAdminAssetService;

    /**
     * 查询资产管理列表
     */
    @SaCheckPermission("oa:adminAsset:list")
    @GetMapping("/list")
    public TableDataInfo<OaAdminAssetVo> list(OaAdminAssetBo bo, PageQuery pageQuery) {
        return iOaAdminAssetService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出资产管理列表
     */
    @SaCheckPermission("oa:adminAsset:export")
    @Log(title = "资产管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaAdminAssetBo bo, HttpServletResponse response) {
        List<OaAdminAssetVo> list = iOaAdminAssetService.queryList(bo);
        ExcelUtil.exportExcel(list, "资产管理", OaAdminAssetVo.class, response);
    }

    /**
     * 获取资产管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("oa:adminAsset:query")
    @GetMapping("/{id}")
    public R<OaAdminAssetVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iOaAdminAssetService.queryById(id));
    }

//    @SaCheckPermission("oa:adminAsset:query")
    @GetMapping("/getAllStandard")
    public R<List<String>> getAllStandard(@RequestParam(value = "assetType", required = false) String assetType) {
        return R.ok(iOaAdminAssetService.getAllStandard(assetType));
    }


//    @SaCheckPermission("oa:adminAsset:query")
    @GetMapping("/getAllProduct")
    public R<List<String>> getAllProduct(@RequestParam(value = "assetType", required = false) String assetType) {
        return R.ok(iOaAdminAssetService.getAllProduct(assetType));
    }

//    @SaCheckPermission("oa:adminAsset:query")
    @GetMapping("/getAllProductNameByAssetCode")
    public R<List<String>> getAllProductNameByAssetCode(@RequestParam(value = "assetType", required = false) String assetType,
                                                        @RequestParam(value = "assetCode", required = false) String assetCode){
        return R.ok(iOaAdminAssetService.getAllProductNameByAssetCode(assetType,assetCode));
    }


//    @SaCheckPermission("oa:adminAsset:query")
    @GetMapping("/getAllAssetCodeByProductName")
    public R<List<String>> getAllAssetCodeByProductName(@RequestParam(value = "assetType", required = false) String assetType,
                                                        @RequestParam(value = "productName", required = false) String productName){
        return R.ok(iOaAdminAssetService.getAllAssetCodeByProductName(assetType,productName));
    }

//    @SaCheckPermission("oa:adminAsset:query")
    @GetMapping("/checkHasOneData")
    public R<OaAdminAsset> checkHasOneData(@RequestParam(value = "assetType", required = false) String assetType,
                                        @RequestParam(value = "assetCode", required = false) String assetCode,
                                        @RequestParam(value = "productName", required = false) String productName){
        return R.ok(iOaAdminAssetService.checkHasOneData(assetType,assetCode,productName));
    }


    @GetMapping("/getAssetHistory")
    public TableDataInfo<OaTotalAssetVo> getAssetHistory(OaAdminEntryBo bo, PageQuery pageQuery) {
        return iOaAdminAssetService.getAssetHistory(bo, pageQuery);
    }


    /**
     * 新增资产管理
     */
    @SaCheckPermission("oa:adminAsset:add")
    @Log(title = "资产管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OaAdminAssetBo bo) {
        return toAjax(iOaAdminAssetService.insertByBo(bo));
    }

    /**
     * 修改资产管理
     */
    @SaCheckPermission("oa:adminAsset:edit")
    @Log(title = "资产管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OaAdminAssetBo bo) {
        return toAjax(iOaAdminAssetService.updateByBo(bo));
    }

    /**
     * 删除资产管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("oa:adminAsset:remove")
    @Log(title = "资产管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iOaAdminAssetService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
